﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Xml.Serialization;
using System.Xml;
using System.Windows.Forms;
using System.Threading;

namespace UnrealDebugger
{
    partial class UnrealDebuggerIDE
    {
        private VersionChecker _versionChecker = null;

        public class VersionInfo
        {
            public string Version { get; set; }
            public string Title { get; set; }
        }

        public class VersionChecker
        {
            private string _title = "";
            private string _version = "";
            private Thread _thread = null;

            public VersionChecker(string title, string version)
            {
                this._title = title;
                this._version = version;

                _thread = new Thread(CheckVersion);
                _thread.Start();
            }

            private void CheckVersion()
            {
                try
                {
                    XmlSerializer xml = new XmlSerializer(typeof(VersionInfo));

                    using (XmlTextReader fs = new XmlTextReader(@"http://unreal-debugger.googlecode.com/svn/trunk/wiki/version.xml"))
                    {
                        VersionInfo info = xml.Deserialize(fs) as VersionInfo;
                        if (info.Version != this._version)
                        {
                            UnrealDebuggerIDE.Instance.Invoke(new MethodInvoker(delegate() { this.ShowAvailableUpdates(info); }));
                        }
                    }
                }
                catch (Exception /*e*/)
                {
                }
            }

            private void ShowAvailableUpdates(VersionInfo info)
            {
                StringBuilder text = new StringBuilder();
                text.AppendFormat("There is a new version of UnrealDebugger available\n\n  Current Version: {0}\n  New Version: {1}\n\nDo you want to download it?",
                    this._version,
                    info.Version);

                DialogResult result = MessageBox.Show(UnrealDebuggerIDE.Instance, text.ToString(), "New version available", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (result == DialogResult.Yes)
                {
                    System.Diagnostics.Process.Start(@"http://code.google.com/p/unreal-debugger/");
                }
            }
        }

        public string Title
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                if (attributes.Length > 0)
                {
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    if (titleAttribute.Title != "")
                    {
                        return titleAttribute.Title;
                    }
                }
                return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
            }
        }

        public string Version
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }
    }
}
